package com.shop.cereshop.admin.job;

import com.shop.cereshop.admin.page.user.BuyerUsersVO;
import com.shop.cereshop.admin.param.buyer.BuyerGetUsersParam;
import com.shop.cereshop.admin.service.buyer.CereBuyerUserService;
import com.shop.cereshop.admin.service.buyer.UserStopSpeeckService;
import com.shop.cereshop.commons.domain.common.Page;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.ZoneId;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Order(10)
@Slf4j
public class UserStopSpeeckJob {

    @Autowired
    private UserStopSpeeckService userStopSpeeckService;

    @Autowired
    private CereBuyerUserService cereBuyerUserService;

    @XxlJob("userStopSpeeck")
    @Scheduled(cron = "0 0/5 * * * ?") // 每 5分钟执行一次
    public void userStopSpeeck() {
        log.info("开始删除用户禁言时间已结束禁言的数据");
        List<BuyerUsersVO> users = cereBuyerUserService.getAllUserToList();

        if (users != null) {
            List<Long> userIds = users.stream()
                    .filter(user -> user.getPrivateChat() != null && user.getPrivateChat() == 0)
                    .filter(user -> user.getGroupChat() != null && user.getGroupChat() == 0)
                    .map(BuyerUsersVO::getBuyerUserId)
                    .collect(Collectors.toList());


            log.info("删除用户id为{}的禁言数据", userIds);
            if (!userIds.isEmpty()){
            Integer i = userStopSpeeckService.deleteByUsers(userIds);
                if (i > 0) {
                    log.info("删除了{}个用户禁言数据", i);
                } else {
                    log.info("没有需要删除的禁言数据");

                }
            }
            else {
                log.info("没有需要删除的禁言数据~~");
            }

        }
        log.info("结束删除用户禁言时间已结束禁言的数据");
    }

    @XxlJob("updateUserStopSpeeckStatus")
    @Scheduled(cron = "0 0/3  * * * ?") // 每 3 分钟执行一次
    public void updateUserStopSpeeckStatus() {
        log.info("开始更新用户禁言状态");
        List<BuyerUsersVO> users = cereBuyerUserService.getAllUserToList();

        if (users != null) {
            // 过滤单聊禁言已结束的用户
            List<Long> privateChatUserIds = users.stream()
                    .filter(user -> user.getPrivateChat()!=null && user.getPrivateChat() == 1 && user.getPrivateChatTime() != null &&
                            user.getPrivateChatTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() < System.currentTimeMillis())
                    .map(BuyerUsersVO::getBuyerUserId)
                    .collect(Collectors.toList());

            // 过滤群聊禁言已结束的用户
            List<Long> groupChatUserIds = users.stream()
                    .filter(user -> user.getGroupChat()!=null && user.getGroupChat() == 1 && user.getGroupChatTime() != null &&
                            user.getGroupChatTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() < System.currentTimeMillis())
                    .map(BuyerUsersVO::getBuyerUserId)
                    .collect(Collectors.toList());

            // 分别更新两种状态
            if (!privateChatUserIds.isEmpty()) {
                log.info("更新{}个用户的单聊禁言状态", privateChatUserIds.size());
                Integer i = userStopSpeeckService.updateUserStopSpeeckStatusPrivate(privateChatUserIds);// 0表示解除禁言
                if (i > 0) {
                    log.info("更新了{}个用户的单聊禁言状态", i);
                } else {
                    log.info("没有需要更新的单聊禁言状态");
                }
            }

            if (!groupChatUserIds.isEmpty()) {
                log.info("更新{}个用户的群聊禁言状态", groupChatUserIds.size());
                userStopSpeeckService.updateUserStopSpeeckStatusGroup(groupChatUserIds); // 0表示解除禁言
            }
        }
    }


}
